package cn.csdj.sism.web;

import cn.csdj.sism.pojo.Class;
import cn.csdj.sism.pojo.Dept;
import cn.csdj.sism.pojo.Employee;
import cn.csdj.sism.service.EmployeeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
/**
 * Controller实现类
 * 谭世豪
 * 2023/4/10
 */
@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public int Insert(@RequestBody Employee employee){
        //如果为空就是添加
        if (StringUtils.isEmpty(employee.getEmpId())){
           return employeeService.insert(employee);
        }else {
//            如果有值就是修改
           return employeeService.update(employee);
        }
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id) {
        return employeeService.deleteById(id);
    }
    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PutMapping
    public int update(@RequestBody Employee employee){
        return employeeService.update(employee);
    }

    /**
     * 查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee selectById(@PathVariable Integer id){
        return employeeService.selectById(id);
    }
    /**
     * 查询全部员工
     * @return
     */
    @GetMapping
    public List<Employee> selectAll(){
        return employeeService.selectAll();
    }
    @GetMapping("/page/{current}/{pageSize}")
    public IPage<Employee> selectPage(@PathVariable Integer current, @PathVariable Integer pageSize){
        return employeeService.selectPages(current,pageSize);
    }
    /**
     * 按条件查询
     * @param chaxun
     * @return
     */
    @PostMapping("/chaxun")
    public IPage<Employee> selectByChaXun(@RequestBody HashMap chaxun){
        return employeeService.selectPagesByChaXun(chaxun);
    }
}
